//package com.yanglanting.disk.mybatis.media;
//
//import com.yanglanting.disk.file.service.FileService;
//import com.yanglanting.disk.mybatis.entity.PtFileEntity;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.core.io.Resource;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.stereotype.Service;
//import com.obs.services.ObsClient;
//import com.obs.services.model.ObsObject;
//import org.apache.commons.io.IOUtils;
//import org.springframework.core.io.InputStreamResource;
//
//import java.io.IOException;
//import java.io.InputStream;
//
//@Service
//public class MediaService {
//
//    @Value("${end.point}")
//    private String endpoint;
//
//    @Value("${ak}")
//    private String ak;
//
//    @Value("${sk}")
//    private String sk;
//
//    @Value("${bucket.name}")
//    private String bucketName;
//
//    @Autowired
//    private FileService fileService;
//
//    public PtFileEntity getFileInfo(String fileUuid) {
//        return fileService.queryFileInfoById(fileUuid);
//    }
//
//    public Resource loadAsResource(PtFileEntity fileEntity) throws IOException {
//        ObsClient obsClient = new ObsClient(ak, sk, endpoint);
//        ObsObject obsObject = obsClient.getObject(bucketName, fileEntity.getFilePath());
//        return new InputStreamResource(obsObject.getObjectContent());
//    }
//
//    public String getContentType(PtFileEntity fileEntity) {
//        String fileName = fileEntity.getFileName().toLowerCase();
//        if (fileName.endsWith(".mp4")) return "video/mp4";
//        if (fileName.endsWith(".avi")) return "video/x-msvideo";
//        if (fileName.endsWith(".mov")) return "video/quicktime";
//        if (fileName.endsWith(".mp3")) return "audio/mpeg";
//        if (fileName.endsWith(".wav")) return "audio/wav";
//        if (fileName.endsWith(".ogg")) return "audio/ogg";
//        return "application/octet-stream";
//    }
//
//    public ResponseEntity<Resource> handlePartialContent(
//            Resource resource, String rangeHeader, long fileSize) {
//
//        try {
//            long rangeStart = 0;
//            long rangeEnd = fileSize - 1;
//            String[] ranges = rangeHeader.substring("bytes=".length()).split("-");
//
//            if (ranges.length >= 1) {
//                rangeStart = Long.parseLong(ranges[0]);
//            }
//            if (ranges.length == 2) {
//                rangeEnd = Long.parseLong(ranges[1]);
//            }
//
//            long contentLength = rangeEnd - rangeStart + 1;
//            InputStream inputStream = resource.getInputStream();
//            inputStream.skip(rangeStart);
//
//            HttpHeaders headers = new HttpHeaders();
//            headers.add("Content-Type", "video/mp4");
//            headers.add("Content-Length", String.valueOf(contentLength));
//            headers.add("Accept-Ranges", "bytes");
//            headers.add("Content-Range", "bytes " + rangeStart + "-" + rangeEnd + "/" + fileSize);
//
//            return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT)
//                    .headers(headers)
//                    .body(new InputStreamResource(inputStream));
//        } catch (IOException e) {
//            throw new RuntimeException("Failed to handle partial content", e);
//        }
//    }
//}
